<template>
	<view>
		<!-- pages/user/index.wxml -->
		<!-- 头部 -->
		<navber id="head-navber" :navbarData="navbarData"></navber>

		<view class="bac">
			<!-- 头像信息 -->
			<view class="head">
				<view class="head-img">
					<image :src="userInfo.head_img ? userInfo.head_img : imgUrl + '/imgs/user/head.png'"></image>
					<view class="head-vip head-vip-open" v-if="loginStatus">{{ userInfo.level_name }}</view>
					<view class="head-vip-img" v-if="userInfo.member_level > 0">
						<image :src="imgUrl + '/imgs/user/vip.png'"></image>
					</view>
				</view>
				<view class="head-info">
					<view class="head-info-name" :style="!loginStatus ? 'text-align:center;' : ''">
						{{ loginStatus ? userInfo.nick_name : '未登录' }}
					</view>
					<view class="head-info-code" v-if="loginStatus">
						<view class="head-info-code-text">邀请码：</view>
						<view class="head-info-code-text1" @tap="copyCode" :data-code="userInfo.popularize_code">
							{{ userInfo.popularize_code }}
						</view>
						<view class="head-info-code-copy" @tap="copyCode" :data-code="userInfo.popularize_code">
							<image :src="imgUrl + '/imgs/user/copy.png'"></image>
						</view>
					</view>
					<view class="head-info-login" v-if="!loginStatus" @tap="userLogin">立即登录</view>
				</view>
			</view>
			<!-- 订单 -->
			<view class="order">
				<view class="order-lable-tip">我的订单</view>
				<view class="order-item-list">
					<view class="order-all" @tap="user_order" data-type="all">
						<view class="order-tip" v-if="false">
							<view>{{ order_data.all_num }}</view>
						</view>
						<view class="order-all-img">
							<image :src="imgUrl + '/imgs/user/order_all.png'"></image>
						</view>
						<view>全部订单</view>
					</view>
					<view class="order-all" @tap="user_order" data-type="dfh">
						<view class="order-tip" v-if="order_data.wait_num > 0">
							<view>{{ order_data.wait_num }}</view>
						</view>
						<view class="order-all-img">
							<image :src="imgUrl + '/imgs/user/order_dfh.png'"></image>
						</view>
						<view>待发货</view>
					</view>
					<view class="order-all" @tap="user_order" data-type="yfh">
						<view class="order-tip" v-if="order_data.send_num > 0">
							<view>{{ order_data.send_num }}</view>
						</view>
						<view class="order-all-img">
							<image :src="imgUrl + '/imgs/user/order_dsh.png'"></image>
						</view>
						<view>已发货</view>
					</view>
				</view>
				<view class="order-logistics" v-if="loginStatus && now_order.traces.AcceptTime">
					<view class="order-logistics-lable">
						<view class="lable-name">最新物流</view>
						<view class="lable-time">{{ now_order.traces.AcceptTime }}</view>
					</view>
					<view class="order-logistics-goods">
						<view class="goods-img">
							<image :src="now_order.goods.goods_img"></image>
						</view>
						<view class="logistics-info">
							<view class="goods-name">{{ now_order.goods.goods_title }}</view>
							<view class="logistics-log">{{ now_order.traces.AcceptStation }}</view>
							<view class="logistics-status">{{ now_order.traces.Label }}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 菜单 -->
		<view class="option" v-if="loginStatus">
			<view class="option-item" @tap="user_profit">
				<view class="option-item-img">
					<image :src="imgUrl + '/imgs/user/profit.png'"></image>
				</view>
				<view class="option-item-text">我的收益</view>
				<view class="option-item-tip">
					<view class="option-item-tip-text"></view>
					<view class="option-item-img-more">
						<image :src="imgUrl + '/imgs/select-right.png'"></image>
					</view>
				</view>
			</view>
			<view class="option-item" @tap="user_address">
				<view class="option-item-img">
					<image :src="imgUrl + '/imgs/user/address.png'"></image>
				</view>
				<view class="option-item-text">地址管理</view>
				<view class="option-item-tip">
					<view class="option-item-tip-text"></view>
					<view class="option-item-img-more">
						<image :src="imgUrl + '/imgs/select-right.png'"></image>
					</view>
				</view>
			</view>
			<view class="option-item" @tap="user_qrcode">
				<view class="option-item-img">
					<image :src="imgUrl + '/imgs/user/user_qrcode.png'"></image>
				</view>
				<view class="option-item-text">推广码</view>
				<view class="option-item-tip">
					<view class="option-item-tip-text"></view>
					<view class="option-item-img-more">
						<image :src="imgUrl + '/imgs/select-right.png'"></image>
					</view>
				</view>
			</view>
			<view class="option-item" @tap="user_popularize">
				<view class="option-item-img">
					<image :src="imgUrl + '/imgs/user/user_popularize.png'"></image>
				</view>
				<view class="option-item-text">我的团队</view>
				<view class="option-item-tip">
					<view class="option-item-tip-text"></view>
					<view class="option-item-img-more">
						<image :src="imgUrl + '/imgs/select-right.png'"></image>
					</view>
				</view>
			</view>
			<view class="option-item" @tap="user_about">
				<view class="option-item-img">
					<image :src="imgUrl + '/imgs/user/user_about.png'"></image>
				</view>
				<view class="option-item-text">关于我们</view>
				<view class="option-item-tip">
					<view class="option-item-tip-text"></view>
					<view class="option-item-img-more">
						<image :src="imgUrl + '/imgs/select-right.png'"></image>
					</view>
				</view>
			</view>
			<view class="option-item" @tap="user_logout">
				<view class="option-item-img">
					<image :src="imgUrl + '/imgs/user/logout.png'"></image>
				</view>
				<view class="option-item-text">退出登录</view>
				<view class="option-item-tip">
					<view class="option-item-tip-text"></view>
					<view class="option-item-img-more">
						<image :src="imgUrl + '/imgs/select-right.png'"></image>
					</view>
				</view>
			</view>
			<view class="invite-popup" v-if="inviteShow">
				<view class="invite-body">
					<view class="invite-title">登陆成功</view>
					<view class="invite-explain">是否填写邀请人信息？</view>
					<input class="invite-intput" :value="inviteCode" @input="inputInviteCode" placeholder="请输入邀请码" />
					<view class="invite-bottom">
						<view class="invite-no-btn" @tap="onCloseInvite">暂不提交</view>
						<view class="invite-btn" @tap="onSubmitInvite">提交</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import navber from '@/components/navber/index';
	// pages/user/index.js
	const Api = require('../../api/index.js');

	const Config = require('../../config/index.js');

	const app = getApp();
	export default {
		components: {
			navber
		},
		data() {
			return {
				navbarData: {
					title: '我的' //顶部标题
				},

				//导航栏
				imgUrl: Config.imgUrl,

				//图片url
				loginStatus: false,

				//登录状态
				userInfo: {
					nick_name: '',
					code: '',
					head_img: false,
					level_name: '',
					member_level: 0,
					popularize_code: ''
				},

				inviteShow: false,
				inviteCode: '',
				member_level: 0,

				order_data: {
					all_num: '',
					wait_num: 0,
					send_num: 0
				},

				now_order: {
					traces: {
						AcceptTime: '',
						AcceptStation: '',
						Label: ''
					},
					goods: {
						goods_img: '',
						goods_title: ''
					}
				}
			};
		},
		onShow() {
			const login_top = uni.getStorageSync('superior_top');

			if (login_top == 'login') {
				this.getData();
			}

			uni.setStorageSync('superior_top', '');
		},
		onLoad() {
			this.getData();
		},
		onPullDownRefresh(e) {
			this.getData();
			uni.stopPullDownRefresh();
		},
		methods: {
			inputInviteCode(e) {
				this.setData({
					inviteCode: e.detail.value
				});
			},

			onCloseInvite() {
				this.setData({
					inviteShow: false
				});
			},

			onSubmitInvite() {
				var json = {
					code: this.inviteCode
				};
				var self = this;
				Api.userTop(json).then((res) => {
					if (res.code == 1) {
						self.setData({
							inviteCode: '',
							inviteShow: false
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				});
			},

			user_logout() {
				const self = this;
				uni.showModal({
					title: '退出登录',
					content: '确认退出登录吗',
					cancelText: '取消',
					cancelColor: '#B2B2B2',
					confirmText: '确认',
					confirmColor: '#EA5757',
					success(res) {
						if (res.confirm) {
							self._logout();
						}
					}
				});
			},

			_logout() {
				uni.removeStorageSync('userInfo');
				uni.removeStorageSync('access_token');
				app.globalData.access_token = null;
				app.globalData.userInfo = {};
				this.getData();
			},

			//邀请用户
			user_invite() {
				this.setData({
					inviteShow: true
				});
			},

			user_popularize() {
				// 粉丝列表
				uni.navigateTo({
					url: '/pages/user/popularize/index'
				});
			},
			user_bean(){
				//流量补贴
				uni.navigateTo({
					url: '/pages/store/index'
				});
			},
			copyCode(e) {
				//复制推广码
				// console.log(e.currentTarget.dataset.code);
				const code = e.currentTarget.dataset.code;
				uni.setClipboardData({
					data: code,
					success(res) {
						uni.getClipboardData({
							success(res) {
								uni.showToast({
								    title: "复制成功",
								    icon: 'none'
								});
							}
						});
					}
				});
			},

			eraRule() {
				//收益规则
				uni.navigateTo({
					url: '/pages/earningsRule/index'
				});
			},

			open_vip(e) {
				//开通vip
				uni.navigateTo({
					url: 'vip/index'
				});
			},

			recommend_goods(e) {
				//推荐商品
				uni.navigateTo({
					url: 'recommend/index'
				});
			},

			collect_goods(e) {
				//收藏商品
				uni.navigateTo({
					url: 'collect/index'
				});
			},

			business_auth() {
				//商家认证
				uni.navigateTo({
					url: 'business/index'
				});
			},

			user_integral() {
				//用户积分
				uni.navigateTo({
					url: 'integral/index'
				}); // wx.navigateTo({
				//   url: '/pages/store/index?id=7',
				// });
			},

			order_statistics() {
				//消费统计
				uni.navigateTo({
					url: '/pages/user/order/statistics/index'
				});
			},

			user_profit() {
				//用户收益
				uni.navigateTo({
					url: 'profit/index'
				});
			},

			user_address() {
				//用户地址
				uni.navigateTo({
					url: 'address/index'
				});
			},

			user_qrcode() {
				//用户推广码
				uni.navigateTo({
					url: 'qrcode/index'
				});
			},

			user_about() {
				//关于我们
				uni.navigateTo({
					url: 'about/index'
				});
			},

			balance_pay() {
				//余额充值
				uni.navigateTo({
					url: 'balancePay/index'
				});
			},

			user_shops() {
				//商家中心
				uni.navigateTo({
					url: '../shops/index'
				});
			},

			userLogin() {
				//用户登录
				// #ifdef MP-WEIXIN
				uni.navigateTo({
					url: '/pages/login/index'
				});
				// #endif
				// uni.navigateTo({
				// 	url: '/pages/login/index'
				// });
				// #ifndef MP-WEIXIN
				uni.navigateTo({
					url: '/pages/login/index'
				})
				// #endif

			},

			user_order(e) {
				uni.navigateTo({
					url: 'order/index?type=' + e.currentTarget.dataset.type
				});
			},

			getData() {
				Api.userCore({
					type: 'user'
				}).then((res) => {
					if (res.code == 1 && res.data) {
						this.setData({
							loginStatus: res.data.is_login,
							userInfo: res.data.userInfos,
							member_level: res.data.userInfos.member_level
						});
						let top_user_id = res.data.userInfos.top_user_id;

						if (parseInt(top_user_id) == 0) {
							this.setData({
								inviteShow: true
							});
						}

						app.globalData.popularize_code = res.data.userInfos.popularize_code;
						app.globalData.userInfo = res.data.userInfos;
					} else {
						this.setData({
							loginStatus: false,
							userInfo: {
								member_level: 0
							},
							member_level: 0
						});
						app.globalData.popularize_code = "";
						app.globalData.userInfo = {
							member_level: 0
						};
					}
				});
			}
		},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {
			const app = getApp();
			// console.log(app.globalData.popularize_code);
			var rand = Math.random() * 100;
			return {
				title: app.globalData.title + '：更多优惠的商品等您来选购哦！',
				path: '/pages/index/index?code=' + app.globalData.popularize_code,
				imageUrl: Config.imgUrl + '/imgs/share.png?rand=' + rand
			};
		},
		onShareTimeline: function() {
			const app = getApp();
			// console.log(app.globalData.popularize_code);
			var rand = Math.random() * 100;
			return {
				title: app.globalData.title + '：更多优惠的商品等您来选购哦！',
				path: '/pages/index/index?code=' + app.globalData.popularize_code,
				imageUrl: Config.imgUrl + '/imgs/share.png?rand=' + rand
			};
		}
	};
</script>
<style>
	@import './index.css';
</style>
